package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.common.Result;
import com.game.yxy.entity.CusFile;
import com.game.yxy.mapper.CusFileMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/cusfile")
@Api(tags = "客户档案管理") // 描述整个控制器的作用
public class CusFileController {
    @Resource
    CusFileMapper cusfileMapper;

    // 添加客户信息
    @PostMapping
    @ApiOperation(value = "添加客户信息", notes = "创建一个新的客户档案记录")
    public Result<?> save(@RequestBody CusFile cusfile){
        cusfileMapper.insert(cusfile);
        return Result.success();
    }

    // 更新客户信息
    @PutMapping
    @ApiOperation(value = "更新客户信息", notes = "根据ID更新客户档案记录")
    public Result<?> update(@RequestBody CusFile cusfile){
        cusfileMapper.updateById(cusfile);
        return Result.success();
    }

    // 删除客户信息
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除客户信息", notes = "根据ID删除客户档案记录")
    public Result<?> delete(@ApiParam(value = "客户档案ID", required = true) @PathVariable Long id){
        cusfileMapper.deleteById(id);
        return Result.success();
    }

    // 查询客户信息
    @GetMapping
    @ApiOperation(value = "查询客户信息", notes = "支持姓名、性别、手机号、关键字查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "姓名", defaultValue = "") @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "性别", defaultValue = "") @RequestParam(defaultValue = "") String sex,
            @ApiParam(value = "手机号", defaultValue = "") @RequestParam(defaultValue = "") String phone,
            @ApiParam(value = "搜索关键词", defaultValue = "") @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<CusFile> wrapper = Wrappers.<CusFile>lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(CusFile::getName, name);
        }
        if (StrUtil.isNotBlank(sex)) {
            wrapper.like(CusFile::getSex, sex);
        }
        if (StrUtil.isNotBlank(phone)) {
            wrapper.like(CusFile::getPhone, phone);
        }
        if (StrUtil.isNotBlank(search)){
            wrapper.like(CusFile::getName,search);
        }
        Page<CusFile> CusFilePage = cusfileMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(CusFilePage);
    }
    // 根据ID查询客户档案
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询客户档案", notes = "根据客户档案ID查询客户档案记录")
    public Result<?> findById(@ApiParam(value = "客户档案ID", required = true) @PathVariable Long id){
        CusFile cusFile = cusfileMapper.selectById(id);
        if (cusFile != null) {
            return Result.success(cusFile);
        } else {
            return Result.failure("客户档案不存在");
        }
    }
}